<?php

if(!defined("IN_PHPBB")) die;

$catid=request_var('catid',0,true);
$action = request_var('action','',true);
$cat = new Category($catid);

//Tao duong dan cho cac category
echo '<h3>';
//categories path
for($j=0;$j<count($cat->path);$j++)
	echo '<a href="index.php?p=items&catid=' . $cat->path[$j]['id'] . '">'. $cat->path[$j]['catname'] . "</a> <strong>&#8249;</strong> ";
echo '<a href="index.php?p=items&catid=' . $cat->data['id'] . '">'. $cat->data['catname'] . '</a></h3><div class="clear"></div>';
//xong
if($action == 'add')
{
    $tag=request_var('tag','',true);
    $name=request_var('name','',true);
    $price=request_var('price',0,true);
    $description=request_var('description','',true);
    $saleoff=request_var('saleoff',0,true);
    $amount=request_var('amount',1,true);
    Item::Add($cat->data['id'],$tag,$name,$price,$description,$amount,$_FILES['image'],$_FILES['images']);
	echo '			<div class="notification success png_bg">
				<a href="#" class="close"><img src="resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
				<div>' .
					$name . " was uploaded successfully!" . '
				</div>
			</div>';
}
?>
<script type="text/javascript">
//<![CDATA[
function add_upload()
{
    element='<br/><input type="file" name="images[]"/>';
    $('#moreimg').append(element);
}
$(document).ready(function(){
		currentTab = $('.content-box ul.content-box-tabs li a.current').attr('href');
		$(currentTab).show();
});
//]]>
</script>
<div class="content-box"><!-- Start Content Box -->
				<div class="content-box-header">
					<h3><?php echo $cat->data['catname']; ?></h3>
                    <ul class="content-box-tabs">
                        <li><a href="#tab3"<? if($action=='view') echo ' class="current"';?>>Items</a></li>
                        <li><a href="#tab1"<? if($action=='') echo ' class="current"';?>>Sub categories</a></li>
                        <li><a href="#tab2"<?php if($action=='add') echo ' class="current"';?>>Add Item</a></li>
                    </ul>
                </div>
                <div class="content-box-content">
                    <div class="tab-content" id="tab1">
						<table>
							
							<thead>
								<tr>
								   <th>Category name</th>
								   <th>Tag</th>
								   <th>Folder</th>
								   <th>More</th>
								</tr>
								
							</thead>						 
							<tbody>
							<?php
								foreach($cat->sub as $name => $row)
								{
									
							?>
								<tr>
									<td><a href="index.php?p=items&catid=<?php echo $row['id']; ?>"><?php echo $row['catname']; ?></a></td>
									<td><a href="#" title="title"><?php echo $row['tag']; ?></a></td>
									<td><?php echo $row['folder']; ?></td>
									<td><a href="index.php?p=items&catid=<?php echo $row['id']; ?>&action=view">View items</a></td>
								</tr>
							<?php
								}
							?>
							</tbody>
							
						</table>
                    </div>
					<div class="tab-content" id="tab2">
                        <form enctype="multipart/form-data" action="index.php?p=items&action=add&catid=<?php echo $catid; ?>" method="post">
                            <fieldset>
                                <p class="column-left">
                                    <label>Item's Name:</label>
                                    <input type="text" class="text-input medium-input" name="name"/>
                                </p>
                                <p class="column-left">
                                    <label>Price:</label>
                                    <input type="text" class="text-input medium-input" name="price" />
                                </p>
                                <p class="column-left">
                                    <label>Sale off:</label>
                                    <input type="text" class="text-input medium-input" name="saleoff" value="0"/>
                                </p>
                                <p class="column-left">
                                    <label>Tag:</label>
                                    <input type="text" class="text-input medium-input" name="tag" value="<?php echo $cat->data['tag']; ?>"/>
                                </p>
								<div class="clear"></div>
                                <!--p class="column-left">
                                    <label>Amount:</label>
                                    <input type="text" class="text-input medium-input" name="amount" value="1"/>
                                </p-->
                                <div class="clear"></div>
								<p>
                                    <label>Description:</label>
                                    <textarea name="description" class="text-input textarea wysiwyg"></textarea>
                                </p>
                                <div class="clear"></div>
                                <p>
                                    <label>Primary Image:</label>
                                    <input type="file" name="image" />
                                </p>
                                <p id="moreimg">
                                    <label>More Images:</label>
                                    <input type="file" name="images[]" />&nbsp;&nbsp;&nbsp;<input class="button" type="button" onclick="add_upload()" value="Add Image"/>
                                </p>
                            </fieldset>
                            <input type="submit" value="Submit" class="button"/>&nbsp;&nbsp;&nbsp;<input type="reset" name="Reset"  class="button"/>
                        </form>
                    </div>
                    <div class="tab-content" id="tab3">
						<form action="index.php?p=items&catid=<?php echo $cat->data['id']; ?>" method="post">
						<table>
							<thead>
								<tr>
									<th>Item's name</th>
									<th>Price</th>
									<th>Amount</th>
									<th>Sale off</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
							<?php
								foreach($cat->items as $item)
								{
									echo '<tr>';
									echo '<td><a href="#">'.$item['name'].'</a></td>';
									echo '<td>'.$item['price'].'</td>';
									echo '<td>'.$item['amount'].'</td>';
									echo '<td>'.$item['saleoff'].'</td>';
									echo '
									<td>
										<!-- Icons -->
										 <a href="index.php?p=items&catid='. $cat->data['id'] . '&tag='.$item['tag'].'&id='.$item['id'].'&action=edit" title="Edit"><img src="resources/images/icons/pencil.png" alt="Edit" /></a>
										 <a href="index.php?p=cat&catid='. $cat->data['id'] . '&tag='.$item['tag'].'&id='.$item['id'].'&action=del" title="Delete"><img src="resources/images/icons/cross.png" alt="Delete" /></a> 
									</td>';
									echo '</tr>';
								}
							?>
							</tbody>
						</table>
						</form>
					</div>
				</div>
</div>